package cn.zjj.rocketmq.consumer;

import cn.zjj.order.domain.OrderPetAcquisition;
import cn.zjj.order.mapper.OrderPetAcquisitionMapper;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * rocketmq 消息监听，@RocketMQMessageListener中的selectorExpression为tag，默认为*
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "queue_test_topic",selectorExpression="*",consumerGroup = "queue_group_test")
public class RocketMQMsgListener implements RocketMQListener<MessageExt> {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Override
    public void onMessage(MessageExt message) {
        String msg = null;
        try {
            byte[] body = message.getBody();
            msg = new String(body, CharsetUtil.UTF_8);
            System.out.println(msg);
            OrderPetAcquisition order = orderPetAcquisitionMapper.selectByOrderSn(msg);
            long time = order.getLastcomfirmtime().getTime();
            long currentTime = System.currentTimeMillis();

            if (order.getState()==0&&(currentTime-time>0)){
                order.setState(-1);
                orderPetAcquisitionMapper.update(order);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        log.info("接收到消息：{}", msg);
    }

}